// Copyright (c) 2006, Gregory Todd <greg@oddsim.com>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
// 
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Othello.Engine
{
    public class Game
    {

        private Player currentPlayer = null;
        private Player whitePlayer = null;
        private Player blackPlayer = null;
        private Board gameBoard = Board.Empty();

        public bool TurnBlack
        {
            get
            {
                if (TurnNone)
                    return false;
                if (currentPlayer == blackPlayer)
                    return true;
                else
                    return false;
            }
        }
        public bool TurnWhite
        {
            get
            {
                if (TurnNone)
                    return false;
                if (currentPlayer == whitePlayer)
                    return true;
                else
                    return false;
            }
        }
        public bool TurnNone
        {
            get
            {
                if (currentPlayer == null)
                    return true;
                else
                    return false;
            }
        }

        public bool TurnHuman
        {
            get
            {
                if (TurnNone)
                    return false;

                if (currentPlayer.computer)
                    return false;
                else
                    return true;
            }
        }
        public bool TurnComputer
        {
            get
            {
                if (TurnNone)
                    return false;

                if (currentPlayer.computer)
                    return true;
                else
                    return false;
            }
        }

        public int PointsBlack
        {
            get { return gameBoard.Points(Board._BLACK); }
        }
        public int PointsWhite
        {
            get { return gameBoard.Points(Board._WHITE); }
        }

        public void Start(Player white, Player black)
        {
            // Set the whitePlayer playerColor
            whitePlayer = white;
            whitePlayer.playerColor = Board._WHITE;

            // Set the blackPlayer playerColor
            blackPlayer = black;
            blackPlayer.playerColor = Board._BLACK;

            // Set inital game state
            gameBoard = Board.Start();
            currentPlayer = blackPlayer;
        }

        public bool IsSpaceBlack(int x, int y)
        {
            if (gameBoard.SpaceColor(x, y) == Board._BLACK)
                return true;
            else
                return false;
        }
        public bool IsSpaceWhite(int x, int y)
        {
            if (gameBoard.SpaceColor(x, y) == Board._WHITE)
                return true;
            else
                return false;
        }
        public bool IsValid(int x, int y)
        {
            if (!TurnNone)
                return gameBoard.CanMove(x, y, currentPlayer.playerColor);
            else
                return false;
        }

        public void PlayHuman(int x, int y)
        {
            if (TurnHuman && gameBoard.CanMove(x, y, currentPlayer.playerColor))
            {
                gameBoard.Move(x, y, currentPlayer.playerColor);
                SwitchTurn();
            }
        }
        public void PlayComputer()
        {
            if (TurnComputer)
            {
                currentPlayer.AlphaBeta(gameBoard);
                SwitchTurn();
            }
        }

        private void SwitchTurn()
        {
            if (gameBoard.GameOver())
                currentPlayer = null;
            else
            {
                if (TurnBlack)
                    currentPlayer = whitePlayer;
                else if (TurnWhite)
                    currentPlayer = blackPlayer;
                if (!gameBoard.CanPlay(currentPlayer.playerColor))
                    SwitchTurn();
            }
        }
    }
}